var map;	// the map itself
var mapOptions;	// map configuration
var geocoder	= new google.maps.Geocoder(); // object to consult google maps server
var latLng;	// current user's location
var loc;	// informations about current location
var infoWindow	= new google.maps.InfoWindow({maxWidth: 300});	// infoWindow
var dmarker;	// draggable marker

function jMaps_initialize() {

	// Default location
	var salvador = new google.maps.LatLng(-12.9833333, -38.5166667);

	mapOptions = {
		zoom: 3,
		language: "en",
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		disableDoubleClickZoom: true,
		panControl: true,
		zoomControl: true,
		mapTypeControl: false,
		streetViewControl: false,
		center: latLng
	}

	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);


  // Get intial location
  if($.jPage_getUrlVar('album')){
    varLat = $.jPage_getUrlVar('lat');
		varLng = $.jPage_getUrlVar('lng');

		latLng = new google.maps.LatLng(varLat,varLng);
		jMaps_initMarker(latLng);
  }
	// Try W3C Geolocation (Preferred)
	else if(navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position) {
			latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			jMaps_initMarker(latLng);
		}, function() {
		jMaps_initMarker(salvador);
	});
	// Try Google Gears Geolocation
	} else if (google.gears) {
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function(position) {
			latLng = new google.maps.LatLng(position.latitude,position.longitude);
			jMaps_initMarker(latLng);
		}, function() {
		jMaps_initMarker(salvador);
	});
	// Browser doesn't support Geolocation
	} else {
		jMaps_initMarker(salvador);
	}

}

function jMaps_initMarker(newLocation) {
	latLng = newLocation;

	map.setCenter(latLng);

	infoWindow.setContent('Olá :)');

  // Global draggable marker

	dmarker = new google.maps.Marker({
		map: map, 
		position: latLng,
		draggable: true,
		zIndex: 9999,
		icon: 'img/music.png',
		shadow: 'img/jammap_shadow.png',
		animation: google.maps.Animation.DROP
	});

  // Map listener

	google.maps.event.addListener(dmarker, 'click', function() {
		infoWindow.open(map, dmarker);
	});

  // Pin listeners

	google.maps.event.addListener(dmarker, 'dragend', function() {
		jMaps_performChooseLocation('dragend');
	});
	
	google.maps.event.addListener(map, 'dblclick', function(event) {
  	dmarker.setPosition(event.latLng);
	  jMaps_performChooseLocation('dblclick');
	});
	
	// Initialize the page simulating user clicks
	jMaps_performChooseLocation('load');
}

function jMaps_performChooseLocation(trackEvent){
	geocoder.geocode({'latLng': dmarker.getPosition(), 'language': mapOptions.language}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		  map.panTo(dmarker.getPosition());
		  loc = results[results.length-1].address_components[0];
		  latLng = dmarker.getPosition();
		  
		  var ad = jAds_getAd(loc.short_name);

		  var totalinfo = 
			  '<div class="infoWindow">' +
			  '<img src="' + 'http://www.fotw.net/images/' +
			  loc.short_name.toLowerCase().charAt(0) + '/' +
			  loc.short_name.toLowerCase() + '.gif" style="vertical-align:center;margin:4px;border: 1px solid #A8A8A8" width="70px" align="left"></img>' +
			  'The best of <br />' +
			  '<a href="javascript:jMaps_performChooseLocation(\'infoViewReload\');">' +
			  loc.long_name + ' - ' + loc.short_name +
			  '</a>' +
			  '<br clear="all" />' +
			  '<hr class="custom_hr" / >' +
			  ad +
			  '<hr class="custom_hr" / >' +
			  '<a href="mailto:cassioso@gmail.com">Promote your b(r)and here.</a>'+
			  '</div>'
			  ;
			
		  infoWindow.setContent(totalinfo);
		  infoWindow.open(map, dmarker);
		
		  jMedia_loadArtists(loc.short_name);
		
		  // analytics
		  _gaq.push(['_trackEvent', 'Map', trackEvent, loc.long_name]);
	  }
	});
}

function jMaps_getAbrev3FromAbrev2(abrev2){
	switch(abrev2){
    case "AF": return "AFG";
    case "AX": return "ALA";
    case "AL": return "ALB";
    case "DZ": return "DZA";
    case "AS": return "ASM";
    case "AD": return "AND";
    case "AO": return "AGO";
    case "AI": return "AIA";
    case "AQ": return "ATA";
    case "AG": return "ATG";
    case "AR": return "ARG";
    case "AM": return "ARM";
    case "AW": return "ABW";
    case "AU": return "AUS";
    case "AT": return "AUT";
    case "AZ": return "AZE";
    case "BS": return "BHS";
    case "BH": return "BHR";
    case "BD": return "BGD";
    case "BB": return "BRB";
    case "BY": return "BLR";
    case "BE": return "BEL";
    case "BZ": return "BLZ";
    case "BJ": return "BEN";
    case "BM": return "BMU";
    case "BT": return "BTN";
    case "BO": return "BOL";
    case "BQ": return "BES";
    case "BA": return "BIH";
    case "BW": return "BWA";
    case "BV": return "BVT";
    case "BR": return "BRA";
    case "IO": return "IOT";
    case "BN": return "BRN";
    case "BG": return "BGR";
    case "BF": return "BFA";
    case "BI": return "BDI";
    case "KH": return "KHM";
    case "CM": return "CMR";
    case "CA": return "CAN";
    case "CV": return "CPV";
    case "KY": return "CYM";
    case "CF": return "CAF";
    case "TD": return "TCD";
    case "CL": return "CHL";
    case "CN": return "CHN";
    case "CX": return "CXR";
    case "CC": return "CCK";
    case "CO": return "COL";
    case "KM": return "COM";
    case "CG": return "COG";
    case "CD": return "COD";
    case "CK": return "COK";
    case "CR": return "CRI";
    case "CI": return "CIV";
    case "HR": return "HRV";
    case "CU": return "CUB";
    case "CW": return "CUW";
    case "CY": return "CYP";
    case "CZ": return "CZE";
    case "DK": return "DNK";
    case "DJ": return "DJI";
    case "DM": return "DMA";
    case "DO": return "DOM";
    case "EC": return "ECU";
    case "EG": return "EGY";
    case "SV": return "SLV";
    case "GQ": return "GNQ";
    case "ER": return "ERI";
    case "EE": return "EST";
    case "ET": return "ETH";
    case "FK": return "FLK";
    case "FO": return "FRO";
    case "FJ": return "FJI";
    case "FI": return "FIN";
    case "FR": return "FRA";
    case "GF": return "GUF";
    case "PF": return "PYF";
    case "TF": return "ATF";
    case "GA": return "GAB";
    case "GM": return "GMB";
    case "GE": return "GEO";
    case "DE": return "DEU";
    case "GH": return "GHA";
    case "GI": return "GIB";
    case "GR": return "GRC";
    case "GL": return "GRL";
    case "GD": return "GRD";
    case "GP": return "GLP";
    case "GU": return "GUM";
    case "GT": return "GTM";
    case "GG": return "GGY";
    case "GN": return "GIN";
    case "GW": return "GNB";
    case "GY": return "GUY";
    case "HT": return "HTI";
    case "HM": return "HMD";
    case "VA": return "VAT";
    case "HN": return "HND";
    case "HK": return "HKG";
    case "HU": return "HUN";
    case "IS": return "ISL";
    case "IN": return "IND";
    case "ID": return "IDN";
    case "IR": return "IRN";
    case "IQ": return "IRQ";
    case "IE": return "IRL";
    case "IM": return "IMN";
    case "IL": return "ISR";
    case "IT": return "ITA";
    case "JM": return "JAM";
    case "JP": return "JPN";
    case "JE": return "JEY";
    case "JO": return "JOR";
    case "KZ": return "KAZ";
    case "KE": return "KEN";
    case "KI": return "KIR";
    case "KP": return "PRK";
    case "KR": return "KOR";
    case "KW": return "KWT";
    case "KG": return "KGZ";
    case "LA": return "LAO";
    case "LV": return "LVA";
    case "LB": return "LBN";
    case "LS": return "LSO";
    case "LR": return "LBR";
    case "LY": return "LBY";
    case "LI": return "LIE";
    case "LT": return "LTU";
    case "LU": return "LUX";
    case "MO": return "MAC";
    case "MK": return "MKD";
    case "MG": return "MDG";
    case "MW": return "MWI";
    case "MY": return "MYS";
    case "MV": return "MDV";
    case "ML": return "MLI";
    case "MT": return "MLT";
    case "MH": return "MHL";
    case "MQ": return "MTQ";
    case "MR": return "MRT";
    case "MU": return "MUS";
    case "YT": return "MYT";
    case "MX": return "MEX";
    case "FM": return "FSM";
    case "MD": return "MDA";
    case "MC": return "MCO";
    case "MN": return "MNG";
    case "ME": return "MNE";
    case "MS": return "MSR";
    case "MA": return "MAR";
    case "MZ": return "MOZ";
    case "MM": return "MMR";
    case "NA": return "NAM";
    case "NR": return "NRU";
    case "NP": return "NPL";
    case "NL": return "NLD";
    case "NC": return "NCL";
    case "NZ": return "NZL";
    case "NI": return "NIC";
    case "NE": return "NER";
    case "NG": return "NGA";
    case "NU": return "NIU";
    case "NF": return "NFK";
    case "MP": return "MNP";
    case "NO": return "NOR";
    case "OM": return "OMN";
    case "PK": return "PAK";
    case "PW": return "PLW";
    case "PS": return "PSE";
    case "PA": return "PAN";
    case "PG": return "PNG";
    case "PY": return "PRY";
    case "PE": return "PER";
    case "PH": return "PHL";
    case "PN": return "PCN";
    case "PL": return "POL";
    case "PT": return "PRT";
    case "PR": return "PRI";
    case "QA": return "QAT";
    case "RE": return "REU";
    case "RO": return "ROU";
    case "RU": return "RUS";
    case "RW": return "RWA";
    case "BL": return "BLM";
    case "SH": return "SHN";
    case "KN": return "KNA";
    case "LC": return "LCA";
    case "MF": return "MAF";
    case "PM": return "SPM";
    case "VC": return "VCT";
    case "WS": return "WSM";
    case "SM": return "SMR";
    case "ST": return "STP";
    case "SA": return "SAU";
    case "SN": return "SEN";
    case "RS": return "SRB";
    case "SC": return "SYC";
    case "SL": return "SLE";
    case "SG": return "SGP";
    case "SX": return "SXM";
    case "SK": return "SVK";
    case "SI": return "SVN";
    case "SB": return "SLB";
    case "SO": return "SOM";
    case "ZA": return "ZAF";
    case "GS": return "SGS";
    case "ES": return "ESP";
    case "LK": return "LKA";
    case "SD": return "SDN";
    case "SR": return "SUR";
    case "SJ": return "SJM";
    case "SZ": return "SWZ";
    case "SE": return "SWE";
    case "CH": return "CHE";
    case "SY": return "SYR";
    case "TW": return "TWN";
    case "TJ": return "TJK";
    case "TZ": return "TZA";
    case "TH": return "THA";
    case "TL": return "TLS";
    case "TG": return "TGO";
    case "TK": return "TKL";
    case "TO": return "TON";
    case "TT": return "TTO";
    case "TN": return "TUN";
    case "TR": return "TUR";
    case "TM": return "TKM";
    case "TC": return "TCA";
    case "TV": return "TUV";
    case "UG": return "UGA";
    case "UA": return "UKR";
    case "AE": return "ARE";
    case "GB": return "GBR";
    case "US": return "USA";
    case "UM": return "UMI";
    case "UY": return "URY";
    case "UZ": return "UZB";
    case "VU": return "VUT";
    case "VE": return "VEN";
    case "VN": return "VNM";
    case "VG": return "VGB";
    case "VI": return "VIR";
    case "WF": return "WLF";
    case "EH": return "ESH";
    case "YE": return "YEM";
    case "ZM": return "ZMB";
    case "ZW": return "ZWE";
		default: return "BRA";
	}
}
